package se.webbzon.boltzmann.menu;

import java.awt.Image;
import java.awt.image.ImageObserver;

import se.webbzon.boltzmann.sound.SoundUtils;
import se.webbzon.oschi01.font.VerticalAlignment;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;

public abstract class MRadio extends MItem {
	
	// The group of this radio button
	private final MRadioGroup group;
	
	// The layout of this radio button
	private final ButtonLayout layout;
	
	// The value of this radio button
	private final Object value;
	
	// The image resource set of the radio button
	private ResourceSet<Image> imageSet;

	// The text of this radio button
	private MText text;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public MRadio(	ImageObserver observer,
							ButtonLayout layout, 
							MRadioGroup group, Object value, 
							boolean isLong, String text) {
		this.layout = layout;
		this.group = group;
		this.value = value;
		this.text = new MText(layout,text);
		this.text.setVAlignment(VerticalAlignment.MIDDLE);
		this.text.setShadow(true);
		if (isLong)
			this.imageSet = layout.getLongButtonImage();
		else
			this.imageSet = layout.getShortButtonImage();
		setWidth(this.imageSet.get(0).getWidth(observer));
		setHeight(this.imageSet.get(0).getHeight(observer));
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	/** Returns the text of this game menu radio button. **/
	public String getText() {
		return text.getText();
	}
	
	/** Sets the text of this game menu radio button. **/
	public void setText(String text) {
		this.text.setText(text);
	}
	
	@Override public void setFocus(boolean focus) {
		if (hasFocus() == false && focus == true)
			SoundUtils.playSound("menu_browse");
		super.setFocus(focus);
	}

	@Override public void onDraw(GameMenu menu, WorldCameraPane hud) {
		int x;
		int y = getY();
		if (hasFocus())
			x = getX() + 8;
		else
			x = getX();
		Object value = group.getValue();
		int index;
		if (value == null)
			index = this.value == null ? 1 : 0;
		else
			index = (value.equals(this.value)) ? 1 : 0;
		hud.drawImage(imageSet.get(index), x, y);
		hud.setFont(layout.getFont());
		hud.setColor(layout.getColor());
		text.draw(hud, x + layout.getTextX(), y + layout.getTextY());
	}

	@Override public void onPress(GameMenu menu, int x, int y) {};

	@Override public void onRelease(GameMenu menu, int x, int y) {
		if (isWithin(x,y)) {
			SoundUtils.playSound("menu_select");
			group.setValue(value);
		}
	}

	@Override public boolean isDraggable() {
		return false;
	}

	@Override public boolean isFocusable() {
		return true;
	}

}
